﻿//-----------------------------------------------------------------------
// <copyright file="ListHelpers.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 

namespace ChatBox.Utility
{
    using System.Collections.Generic;
    using System.Linq;

    /// <summary>
    /// Helper methods on lists and enumerations
    /// </summary>
    public static class ListHelpers
    {
        /// <summary>
        /// True if the list contains at least one item
        /// </summary>
        /// <typeparam name="T">type of item in the list</typeparam>
        /// <param name="items">the items to inspect</param>
        /// <returns>true if the list contains at leat one item</returns>
        public static bool HasItems<T>(this IEnumerable<T> items)
        {
            var enumerator = items.GetEnumerator();

            // return true if MoveNext indicates that there is a first item
            return enumerator.MoveNext();
        }

        /// <summary>
        /// Trim the list to the last few items
        /// </summary>
        /// <typeparam name="T">the type of item in the list</typeparam>
        /// <param name="list">the list to trim</param>
        /// <param name="count">the number of items to retain from the end of the list</param>
        /// <returns>the trimmed list</returns>
        public static IList<T> TakeLast<T>(this IList<T> list, int count)
        {
            if (list.Count <= count)
            {
                return list;
            }

            return list.Skip(list.Count - count).ToList();
        }

        /// <summary>
        /// Trim the enumerable to the last few items
        /// </summary>
        /// <typeparam name="T">the type of item in the enumerable</typeparam>
        /// <param name="enumerable">the list to trim</param>
        /// <param name="count">the number of items to retain from the end of the enumerable</param>
        /// <returns>the trimmed enumerable</returns>
        public static IEnumerable<T> TakeLast<T>(this IEnumerable<T> enumerable, int count)
        {
            int listCount = enumerable.Count();
            if (listCount <= count)
            {
                return enumerable;
            }
            else
            {
                return enumerable.Skip(listCount - count);
            }
        }

        /// <summary>
        /// Check if the two lists contain the same elements
        /// </summary>
        /// <typeparam name="T">the type of item in the list</typeparam>
        /// <param name="list">the list of items</param>
        /// <param name="otherList">the other list to companre</param>
        /// <returns>true if the lists are the same</returns>
        public static bool ListSame<T>(this IEnumerable<T> list, IEnumerable<T> otherList)
        {
            if (list.Count() != otherList.Count())
            {
                return false;
            }

            if (!list.All(item => otherList.Contains(item)))
            {
                return false;
            }

            if (!otherList.All(item => list.Contains(item)))
            {
                return false;
            }

            return true;
        }
    }
}
